using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _2DGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Background
    {
        class Layer
        {
            public String filename;
            public Texture2D texture;
        }

        Layer[] layers;

        public Background(int numLayers)
        {
            layers = new Layer[numLayers];
            for (int i = 0; i < numLayers; ++i)
            {
                layers[i] = new Layer();
            }
        }

        public void SetLayer(int index, String filename)
        {
            layers[index].filename = filename;

            // Make a new entry for this texture in the texture bank if it doesn't exist
            // Textures will be loaded into the bank during World.LoadContent
            if (Resources.TextureBank.TryGetValue(filename, out layers[index].texture) == false)
            {
                Resources.TextureBank.Add(filename, null);
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            // TODO: Add your initialization code here
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        public void LoadContent()
        {
            foreach (Layer layer in layers)
            {
                layer.texture = Resources.TextureBank[layer.filename];
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
        }

        public void Draw()
        {
            Rectangle sourceRect = new Rectangle();

            sourceRect.X = -(Resources.Device.Viewport.Width / 2);
            sourceRect.Y = -(Resources.Device.Viewport.Height / 2);
            sourceRect.Width = Resources.Device.Viewport.Width;
            sourceRect.Height = Resources.Device.Viewport.Height;

            // Draw a big black poly for now. Will use some parallax stuff later.
            Resources.SpriteBatch.Draw(
                layers[0].texture,
                new Rectangle(0, 
                    0, 
                    Resources.Device.Viewport.Width, 
                    Resources.Device.Viewport.Height), 
                Color.White);

            sourceRect.X = (int)Math.Round(Resources.World.CameraPosition.X / 2) - (Resources.Device.Viewport.Width / 2);
            sourceRect.Y = (int)Math.Round(Resources.World.CameraPosition.Y / 2) - (Resources.Device.Viewport.Height / 2);
            sourceRect.Width = Resources.Device.Viewport.Width;
            sourceRect.Height = Resources.Device.Viewport.Height;

            // Draw a big black poly for now. Will use some parallax stuff later.
            Resources.SpriteBatch.Draw(
                layers[1].texture,
                new Rectangle(0,
                    0,
                    Resources.Device.Viewport.Width,
                    Resources.Device.Viewport.Height),
                sourceRect,
                Color.White);
        }
    }
}